package com.woniuxy.controller;

import com.woniuxy.entity.dto.operations.CostRulesDetailDTO;
import com.woniuxy.entity.dto.operations.CostRulesDisplayDTO;
import com.woniuxy.entity.po.operations.BindCostAndTypeRules;
import com.woniuxy.service.CostRulesService;
import com.woniuxy.util.PageParam;
import com.woniuxy.util.ResponseData;
import com.woniuxy.util.TimeRuleValidatorUtil;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @BelongsProject: charging-management-platform
 * @BelongsPackage: com.woniuxy.controller
 * @Author: Quan
 * @Date: 2025/4/3 14:13
 */
@RestController
@RequestMapping("/costRules")
@AllArgsConstructor
public class CostRulesController {
	private CostRulesService costRulesService;

	/**
	 * @Description: 获取收费规则
	 * @Param: []
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/5
	 */
	@GetMapping("/getChargingStage")
	public ResponseData<?> getChargingStage() {
		return costRulesService.getChargingStage();
	}

	/**
	 * @Description: 将规则添加到cost_rules和bind_cost_and_type_rules中
	 * @Param: [costRulesDetailDTO]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/6
	 */
	@PostMapping("/addCostRules")
	public ResponseData<?> addCostRules(@RequestBody CostRulesDetailDTO costRulesDetailDTO) {
		List<BindCostAndTypeRules> rulesList = costRulesDetailDTO.getBindCostAndTypeRulesList();
		// 验证其中的规则队列的合法性
		TimeRuleValidatorUtil.validateTimeRules(rulesList);

		return costRulesService.addCostRules(costRulesDetailDTO);
	}

	/**
	 * @Description: 进行条件查询
	 * @Param: [pageParam]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/6
	 */
	@PostMapping("/queryAll")
	public ResponseData<?> queryAll(@RequestBody PageParam<CostRulesDisplayDTO> pageParam) {
		return costRulesService.queryAll(pageParam);
	}

	/**
	 * @Description: 通过id查询规则的细节，并且返回到查看页面
	 * @Param: [id]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/6
	 */
	@GetMapping("/queryById/{id}")
	public ResponseData<?> queryById(@PathVariable("id") Long id) {
		return ResponseData.ok(costRulesService.queryById(id));
	}

	/**
	 * @Description: 通过id进行删除
	 * @Param:
	 * @return:
	 * @Author: Quan
	 * @Date: 2025/4/6
	 */
	@GetMapping("/deleteById/{id}")
	public ResponseData<?> deleteById(@PathVariable("id") Long id) {
		return costRulesService.deleteById(id);
	}

	/**
	 * @Description: 更新消费规则的状态（禁用=》启用，启用=》禁用）
	 * @Param: [costRulesDetailDTO]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/6
	 */
	@PostMapping("/updateCostRulesCondition")
	public ResponseData<?> updateCostRulesCondition(@RequestBody CostRulesDetailDTO costRulesDetailDTO) {
		return costRulesService.updateCostRulesCondition(costRulesDetailDTO);
	}

	/**
	 * @Description: 通过
	 * @Param: [costRulesDetailDTO]
	 * @return: com.woniuxy.util.ResponseData<?>
	 * @Author: Quan
	 * @Date: 2025/4/6
	 */
	@PostMapping("/updateCostRules")
	public ResponseData<?> updateCostRules(@RequestBody CostRulesDetailDTO costRulesDetailDTO) {
		return costRulesService.updateCostRules(costRulesDetailDTO);
	}
}
